package tq.com.easysurfacecapture.Inner;

import android.graphics.Path;
import android.graphics.PointF;

import java.util.ArrayList;
import java.util.List;

public class TQPathHold {
    private PointF offset = new PointF(0f, 0f);
    private Path currentPath = new Path();

    private List<PointF> set = new ArrayList<>();
    private boolean isEnd = false;

    public TQPathHold(PointF pos){
        startPoint(pos);
    }

    public TQPathHold(){

    }

    public void startPoint(PointF pos){
        currentPath.reset();

        set.clear();
        currentPath.reset();
        isEnd = false;

        set.add(pos);
    }

    public void addPoint(PointF pos){
        set.add(pos);
    }
    public void endPoint(PointF pos){
        set.add(pos);
        isEnd = true;
    }

    public Path getPath(PointF offset){
        if(isEnd && offset.equals(this.offset) && !currentPath.isEmpty()){
            return currentPath;
        }

        currentPath.reset();
        for (int i = 0; i < set.size(); i++){
            PointF pos = set.get(i);
            PointF need = needPosition(pos, offset);

            if(i == 0){
                currentPath.moveTo(need.x, need.y);
            }else if(i == (set.size() -1)){
              currentPath.lineTo(need.x, need.y);
            } else{
                currentPath.lineTo(need.x, need.y);
            }
        }

        return currentPath;
    }

    private PointF needPosition(PointF pos, PointF offset){
        PointF p = new PointF(pos.x, pos.y);
        p.offset(offset.x, offset.y);
        return p;
    }
}
